package cn.java86.myschool.web;

import cn.java86.myschool.entity.Page;
import cn.java86.myschool.entity.Subject;
import cn.java86.myschool.service.GradeServiceI;
import cn.java86.myschool.service.SubjectServiceI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * @author oldliu
 * @since 1.0
 */
@Controller
@RequestMapping("subject")
public class SubjectController {

    @Autowired
    private SubjectServiceI subjectService;
    @Autowired
    private GradeServiceI gradeService;
    //private SubjectDao subjectDao;

    // @GetMapping("findall")
//   // public List<Subject> findall(){
//        return subjectDao.findAll();
//    }
    //html地址
    //Model保存模型（业务逻辑层）传递过来的数据
    @GetMapping("list")
    public String search(HttpServletRequest request,
                         Model model,
                         @RequestParam(name = "pageNo", defaultValue = "1") int pageNo,
                         @RequestParam(name = "pageSize", defaultValue = "10") int pageSize,
                         @RequestParam(name = "name", required = false) String name) {
        /*List<Subject> list = this.subjectDao.search(pageNo, pageSize, name);
        int total=this.subjectDao.findTotal(name);*/
        //String xx = request.getParameter("xx");
//        String ip = request.getRemoteAddr();
//        System.out.println("客户端ip是:" + ip+"  name:"+name);

        Page<Subject> p = subjectService.search(pageNo, pageSize, name);
        //到html中显示
        model.addAttribute("p", p);
        model.addAttribute("name", name);
        return "subject/list";//html地址
    }

    @GetMapping("toAdd")
    public String toAdd(Model model) {
        model.addAttribute("grades", gradeService.findAll());

        return "subject/add";
    }

    //请求参数和对象的属性一致，自动绑定
    @PostMapping("add")
    public String add(Subject sub/*,HttpServletRequest request*/) {
        int r = this.subjectService.add(sub);
        if (r > 0) {
            //成功
            //return "subject/list";
            //重定向
            return "redirect:/subject/list";
        }
        return "fail";

//        String name=request.getParameter("xxxname");
//        Subject s=new Subject();
//        s.setSubjectname(name);
    }

    @GetMapping("del")
    public String del(long id) {
        int d = this.subjectService.del(id);
        return "redirect:/subject/list";
        //d==0
    }

    @GetMapping("toUpdate")
    public String toUpdate(long id, Model model) {
        Subject subject = this.subjectService.get(id);
        if (subject == null) {
            return "fail";
        }
        model.addAttribute("subject", subject);
        model.addAttribute("grades", gradeService.findAll());
        return "subject/update";
        //d==0
    }

    @PostMapping("update")
    public String update(Subject sub/*,HttpServletRequest request*/) {
        int r = this.subjectService.update(sub);
        if (r > 0) {
            //成功
            //return "subject/list";
            //重定向
            return "redirect:/subject/list";
        }
        return "fail";

    }
}
